package com.nogroup.springbootproject.service.impl;

import com.nogroup.springbootproject.entity.Office;
import com.nogroup.springbootproject.dao.OfficeDao;
import com.nogroup.springbootproject.service.OfficeService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * (Office)表服务实现类
 *
 * @author makejava
 * @since 2024-04-07 18:45:33
 */
@Service("officeService")
@Transactional(rollbackFor = Exception.class)
public class OfficeServiceImpl implements OfficeService {
    @Resource
    private OfficeDao officeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param officeCode 主键
     * @return 实例对象
     */
    @Override
    public Office queryById(String officeCode) {
        return this.officeDao.queryById(officeCode);
    }

    /**
     * 分页查询
     *
     * @param office 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Office> queryByPage(Office office, PageRequest pageRequest) {
        long total = this.officeDao.count(office);
        return new PageImpl<>(this.officeDao.queryAllByLimit(office, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param office 实例对象
     * @return 实例对象
     */
    @Override
    public Office insert(Office office) {
        this.officeDao.insert(office);
        return office;
    }

    /**
     * 修改数据
     *
     * @param office 实例对象
     * @return 实例对象
     */
    @Override
    public Office update(Office office) {
        this.officeDao.update(office);
        return this.queryById(office.getOfficeCode());
    }

    /**
     * 通过主键删除数据
     *
     * @param officeCode 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String officeCode) {
        return this.officeDao.deleteById(officeCode) > 0;
    }
}
